package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcFollowDataEcgMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFollowDataBpressure;
import com.csun.cmny.provider.model.domain.DmcFollowDataEcg;
import com.csun.cmny.provider.model.dto.follow.FollowDataPageQuery;
import com.csun.cmny.provider.model.dto.follow.UpFollowDataEcgDto;
import com.csun.cmny.provider.model.enums.follow.FollowBpressureStatusEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFollowDataEcgService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 21:46
 * @Description: the class dmc follow data ecg service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFollowDataEcgServiceImpl extends BaseService<DmcFollowDataEcg>
        implements DmcFollowDataEcgService {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataEcgMapper followDataEcgDao;

    /**
     * query dmc follow data ecg list with page
     *
     * @param followDataPageQuery the follow data query page
     *
     * @return the page info
     */
    @Override
    public PageInfo queryDmcFollowDataEcgListWithPage(FollowDataPageQuery followDataPageQuery) {

        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<DmcFollowDataEcg> dmcFollowDataEcgList = followDataEcgDao
                .queryDmcFollowDataEcgListWithPage(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataEcgList);
    }

    /**
     * up follow data ecg
     *
     * @param upFollowDataEcgDto up follow data ecg dto
     * @param loginAuthDto       login auth dto
     * @return the int.
     */
    @Override
    public Integer upFollowDataEcg(UpFollowDataEcgDto upFollowDataEcgDto, LoginAuthDto loginAuthDto) {

        // 1.校验设备
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsFollowByDeviceId(upFollowDataEcgDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610041, upFollowDataEcgDto.getDeviceId());
        }

        // 2.校验参数
        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataEcgDto.getEcg()),
                cmnyMessageSource.getMessage("follow.up.ecg.null", null));

        // 3.插入数据
        DmcFollowDataEcg dmcFollowDataEcg = new DmcFollowDataEcg();
        BeanUtils.copyProperties(upFollowDataEcgDto, dmcFollowDataEcg);
        dmcFollowDataEcg.setUpdateInfo(loginAuthDto);

        return followDataEcgDao.insertSelective(dmcFollowDataEcg);

    }

    @Override
    public Map<String, Object> getRecentData(Integer oldmanId) {
        return followDataEcgDao.getRecentData(oldmanId);
    }

    @Override
    public List<Map<String, Object>> getAllOldmanData(Integer oldmanId) {
        return followDataEcgDao.getAllOldmanData(oldmanId);
    }
}
